<template>
  <el-form ref="damageNoticeForm" :model="damageNoticeForm">
    <el-form-item v-if="noticeStatus!==0">
      <el-row>
        <el-col :span="12">
          <el-form-item label="通知号:">
            <span v-show="showTag">{{ damageNoticeData.damageNoticeID }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="通知时间:">
            <span v-show="showTag">{{ toDateString(damageNoticeData.noticeDate) }}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="审核状态:">
            <el-tag
              v-show="showTag"
              :type="getStatusColor()"
            >{{ convertStatus() }}</el-tag>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="审核意见:">
            <span>{{ damageNoticeData.auditOpinion }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form-item>
    <el-form-item prop="facilityName" :rules="[{ required: true, message: '必须填写设施名称'}]" label="设施名称">
      <el-input
        v-model="damageNoticeForm.facilityName"
        type="textarea"
        autosize
        :disabled="noticeStatus===2"
        placeholder="请输入设施名称"
      />
    </el-form-item>
    <el-form-item prop="damagePart" :rules="[{ required: true, message: '必须填写损坏部位'}]" label="损坏部位">
      <el-input
        v-model="damageNoticeForm.damagePart"
        type="textarea"
        autosize
        :disabled="noticeStatus===2"
        placeholder="请输入损坏部位"
      />
    </el-form-item>
    <el-form-item prop="damageReason" :rules="[{ required: true, message: '必须填写损坏原因'}]" label="损坏原因">
      <el-input
        v-model="damageNoticeForm.damageReason"
        type="textarea"
        autosize
        :disabled="noticeStatus===2"
        placeholder="请输入损坏原因"
      />
    </el-form-item>
    <el-row v-if="noticeStatus!==2" type="flex" justify="end">
      <el-button type="primary" :loading="loading" @click="submit('damageNoticeForm')">提 交</el-button>
    </el-row>
  </el-form>
</template>

<script>
import { searchDamageNotice, updateDamageNotice, addDamageNotice } from '@/api/dailyInspection'
import { toDateString } from '@/utils/time-handle'

export default {
  name: 'DamageNotice',
  props: {
    noticeStatus: {
      type: Number,
      default: 0
    },
    dailyInspectId: {
      type: Number,
      default: -1
    },
    damageNoticeId: {
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      damageNoticeForm: {
        facilityName: '',
        damagePart: '',
        damageReason: ''
      },
      damageNoticeData: {},
      showTag: false,
      loading: false
    }
  },
  created() {
    if (this.noticeStatus !== 0) {
      this.getData()
    }
  },
  methods: {
    toDateString,
    submit(formName) {
      this.loading = true
      this.$refs[formName].validate((result) => {
        if (result) {
          if (!this.noticeStatus) {
            this.damageNoticeForm.dailyInspecteID = this.dailyInspectId
            addDamageNotice(this.damageNoticeForm).then(() => {
              this.$emit('success')
              this.$message.success('提交成功')
            })
          } else {
            this.damageNoticeForm.damageNoticeID = this.damageNoticeId
            updateDamageNotice(this.damageNoticeForm).then(() => {
              this.$emit('success')
              this.$message.success('修改成功')
            })
          }
        } else {
          this.loading = false
          this.$message.error('请检查表单必填项')
          return false
        }
      })
    },
    getData() {
      searchDamageNotice({ id: this.damageNoticeId }).then(data => {
        this.damageNoticeData = data
        this.damageNoticeForm.facilityName = data.facilityName
        this.damageNoticeForm.damagePart = data.damagePart
        this.damageNoticeForm.damageReason = data.damageReason
        this.showTag = true
      })
    },
    convertStatus() {
      switch (this.damageNoticeData.auditStatus) {
        case 0:
          return '未审核'
        case 1:
          return '已通过'
        case 2:
          return '未通过'
      }
    },
    getStatusColor() {
      switch (this.damageNoticeData.auditStatus) {
        case 0:
          return 'info'
        case 1:
          return 'success'
        case 2:
          return 'danger'
      }
    }
  }
}
</script>

<style scoped>

</style>
